
package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;


import model.core.torneo.Torneo;
import model.core.utente.Giocatore;
import model.core.utente.Studente;
import model.persistence.mysql.DAOManager_MySQL;

/**
 * 
 * @author Giuseppe Fusco (fusgius@gmail.com)
 *
 */
public class PartecipantiSingoliTorneo extends javax.swing.JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6194945789459788078L;

	private DAOManager_MySQL dao;
	private List<Integer> index;
	private List<Studente> studenti;
	private Torneo torneo;

	public PartecipantiSingoliTorneo(Torneo torneo) {
		initComponents();
		try {
			this.torneo = torneo;
			dao = DAOManager_MySQL.getInstance();
			index = new ArrayList<Integer>();
			studenti = new ArrayList<Studente>();
			fillComponents("");
			addListener();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	private void fillComponents(String text){
		try {
			studenti = dao.getStudenteDao().load(text);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		listPartecipanti.setCellRenderer(new StudenteCellRenderer(index));
		listPartecipanti.setModel(new StudenteListModel(studenti));
		totPartecipanti.setText(String.valueOf(listPartecipanti.getModel().getSize()));
	}

	private void addListener(){

		fatto.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		/*cerca.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				fillComponents(cercaPartecipante.getText());
			}
		});*/

		aggiungiPartecipante.addActionListener(new ActionListener() {

			@SuppressWarnings("unchecked")
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if(listPartecipanti.getSelectedIndex()>=0){
					int id = listPartecipanti.getSelectedIndex();
					if(torneo.addGiocatore(Giocatore.createFromStudente(studenti.get(id)))){
						index.add(new Integer(id));
						listPartecipanti.setCellRenderer(new StudenteCellRenderer(index));
					}
					else
						JOptionPane.showMessageDialog(getParent(), "Hai raggionto il numero massimo di partecipanti.");
				}
			}
		});

		eliminaPartecipante.addActionListener(new ActionListener() {

			@SuppressWarnings("unchecked")
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if(listPartecipanti.getSelectedIndex()>=0){
					int id = listPartecipanti.getSelectedIndex();
					if(torneo.removeGiocatore(Giocatore.createFromStudente(studenti.get(id)))){
						index.remove(new Integer(id));
						listPartecipanti.setCellRenderer(new StudenteCellRenderer(index));
					}
					else
						JOptionPane.showMessageDialog(getParent(), "Hai raggionto il numero minimo di partecipanti.");
				}
			}
		});
	}

	@SuppressWarnings("rawtypes")
	private void initComponents() {

		jScrollPane2 = new javax.swing.JScrollPane();
		listPartecipanti = new javax.swing.JList();
		javax.swing.JLabel jLabel8 = new javax.swing.JLabel();
		totPartecipanti = new javax.swing.JTextField();
		javax.swing.JLabel jLabel11 = new javax.swing.JLabel();
		eliminaPartecipante = new javax.swing.JButton();
		aggiungiPartecipante = new javax.swing.JButton();
		cercaPartecipante = new javax.swing.JTextField();
		annulla = new javax.swing.JButton();
		fatto = new javax.swing.JButton();
		cerca = new javax.swing.JButton();

		listPartecipanti.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N
		listPartecipanti.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
		jScrollPane2.setViewportView(listPartecipanti);

		jLabel8.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N
		jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		jLabel8.setText("Partecipanti");

		totPartecipanti.setEditable(false);
		totPartecipanti.setColumns(4);
		totPartecipanti.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N

		jLabel11.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N
		jLabel11.setText("Totale");

		eliminaPartecipante.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N
		eliminaPartecipante.setText("Elimina");

		aggiungiPartecipante.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N
		aggiungiPartecipante.setText("Aggiungi");

		cercaPartecipante.setColumns(4);
		cercaPartecipante.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N

		annulla.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N
		annulla.setText("Annulla");

		fatto.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N
		fatto.setText("Fatto");

		cerca.setFont(new java.awt.Font("Trebuchet MS", 1, 12)); // NOI18N
		cerca.setText("Cerca");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(layout.createSequentialGroup()
										.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(layout.createSequentialGroup()
														.addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
														.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
														.addComponent(jLabel11)
														.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
														.addComponent(totPartecipanti, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
														.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
																.addGap(119, 119, 119)
																.addComponent(cerca)
																.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addComponent(cercaPartecipante))
																.addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE))
																.addGap(5, 5, 5)
																.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
																		.addComponent(eliminaPartecipante, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
																		.addComponent(aggiungiPartecipante, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)))
																		.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
																				.addGap(0, 0, Short.MAX_VALUE)
																				.addComponent(annulla)
																				.addGap(0, 0, 0)
																				.addComponent(fatto)))
																				.addContainerGap())
				);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(jLabel8)
								.addComponent(jLabel11)
								.addComponent(totPartecipanti, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
										.addGroup(layout.createSequentialGroup()
												.addComponent(aggiungiPartecipante)
												.addGap(0, 0, 0)
												.addComponent(eliminaPartecipante)
												.addGap(0, 0, Short.MAX_VALUE))
												.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE))
												.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
												.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(cercaPartecipante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(cerca))
														.addGap(30, 30, 30)
														.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
																.addComponent(annulla)
																.addComponent(fatto))
																.addContainerGap())
				);
	}

	private javax.swing.JButton aggiungiPartecipante;
	private javax.swing.JButton annulla;
	private javax.swing.JButton cerca;
	private javax.swing.JTextField cercaPartecipante;
	private javax.swing.JButton eliminaPartecipante;
	private javax.swing.JButton fatto;
	private javax.swing.JScrollPane jScrollPane2;
	@SuppressWarnings("rawtypes")
	private javax.swing.JList listPartecipanti;
	private javax.swing.JTextField totPartecipanti;
}
